import {Injectable} from '@angular/core';
import {Observable} from "rxjs";
import {map} from 'rxjs/operators';
import {User} from "./user";
import {HttpClient} from "@angular/common/http";
import {Result} from "../result";


@Injectable({
  providedIn: 'root'
})

export class UserService {


  constructor(private http: HttpClient) {
  }

  getUsers(): Observable<User[]> {
    return this.http.get<Result>("api/demo/userList")
      .pipe(
        map(({data}) => data)
      )
  }

  getUser(id: number | string) {
    return this.getUsers().pipe(
      map(users => users.find(user => user.id === +id)!)
    );
  }


}
